package com.example.mobile_caijain0626;

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import com.bumptech.glide.Glide;
import com.google.android.material.bottomappbar.BottomAppBar;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.theartofdev.edmodo.cropper.CropImageView;

import java.io.File;
import java.lang.invoke.ConstantCallSite;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.logging.SimpleFormatter;

public class MainActivity extends AppCompatActivity {
    //SimpleDateFormat, 是一种简单的格式, 构造时需要传入指定的参数, 包含参数名称和格式, 以匹配需要解析的字符串;
    private SimpleDateFormat simpleDateFormat=new SimpleDateFormat("HHmmss", Locale.CHINA);
    private ImageView imageView;
    private Button button;
    private int REQUEST_CAMERA=110;
    private File photoFile=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }


    private void initView() {
        imageView = (ImageView) findViewById(R.id.imageView);
        button = (Button) findViewById(R.id.button);
    }
    public void init(View view){
        imageView = (ImageView) findViewById(R.id.imageView);
        button = (Button) findViewById(R.id.button);
        String[] name={"拍照","相册","退出"};
        ListView listView=new ListView(this);
        listView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
        listView.setAdapter(new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,name));
        BottomSheetDialog dialog=new BottomSheetDialog(this);//底部弹窗
        dialog.setContentView(listView);
        imageView.setOnClickListener(v -> {
            dialog.show();
        });
        listView.setOnItemClickListener((parent, view1, position, id) -> {
            switch (position){
                case 0:
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    // 这个File 是用作保存拍照后的照片的,后面会将这个File设置为Intent的输出文件
                    photoFile= new File(this.getExternalCacheDir(), "test.png");
                    // android 7.0 后禁止使用file://uri  需要使用content://uri
                    // 将上面定义的file文件 转成合适的uri
                    Uri uri = FileProvider.getUriForFile(this, "com.example.mobile_caijain0626.fileProvider", photoFile);
                    // 将输出的文件uri 应用到intent中。
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                    // 启动intent并等待返回值
                    startActivityForResult(intent, REQUEST_CAMERA);
                    break;
                case 1:
                    Intent intent_1 = new Intent(Intent.ACTION_PICK);
                    intent_1.setType("image/*");
                    startActivityForResult(intent_1, 111);
                    break;
                case 2:
                    dialog.dismiss();
                    break;
                default:
                    break;
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK){
            if (requestCode==REQUEST_CAMERA){
                Glide.with(this).load(photoFile).into(imageView);
            }else if (requestCode==111){
                try {
                    Uri uri=data.getData();
                    ContentResolver contentResolver=getContentResolver();
                    String [] colm={MediaStore.Images.ImageColumns.DATA};
                }catch (NullPointerException e){
                    e.printStackTrace();
                }

            }
        }
    }
}